package com.xth8013.as.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xth8013.as.domain.User;
import com.xth8013.as.service.UserService;
import com.xth8013.as.util.Message;
import com.xth8013.as.util.ObjectUtils;
import com.xth8013.as.util.UserQueryHelper;

/**
 * 
 * @Description:
 * @author: tianhua.xie
 * @Date: 2017年4月3日 下午9:39:15
 *
 */
@Controller
@RequestMapping("/userMgrController")
public class UserMgrController extends BaseController {

	@Autowired
	private UserService userService;

	@RequestMapping("/userMgr")
	public String loadUserList() {

		return "user/user_list";
	}

	@RequestMapping(value = "/userListJson")
	@ResponseBody
	public Map<String, Object> userListJson(UserQueryHelper helper, HttpServletRequest request) {

		if (ObjectUtils.isBlank(helper)) {
			helper = new UserQueryHelper();
			helper.setQryUserStatus("1");
		}
		System.out.println(helper);

		// String pageNo = request.getParameter("page");
		// String pageSize = request.getParameter("rows");
		// String qryAccount = request.getParameter("qryAccount");
		// String qryName = request.getParameter("qryName");
		// String qryDepartmentId = request.getParameter("qryDepartmentId");
		// String qryUserStatus = request.getParameter("qryUserStatus");
		// String orderField = request.getParameter("sort");
		// String orderType = request.getParameter("order");
		//
		// if (ObjectUtils.isNotBlank(pageNo)) {
		// helper.setPageNo(Integer.parseInt(pageNo));
		// }
		// if (ObjectUtils.isNotBlank(pageSize)) {
		// helper.setPageSize(Integer.parseInt(pageSize));
		// }
		// if (ObjectUtils.isNotBlank(qryAccount)) {
		// helper.setQryAccount(qryAccount);
		// }
		// if (ObjectUtils.isNotBlank(qryName)) {
		// helper.setQryName(qryName);
		// }
		// if (ObjectUtils.isNotBlank(qryDepartmentId)) {
		// helper.setQryDepartmentId(Integer.parseInt(qryDepartmentId));
		// }
		// if (ObjectUtils.isNotBlank(qryUserStatus)) {
		// helper.setQryUserStatus(qryUserStatus);
		// }
		// if (ObjectUtils.isNotBlank(orderField)) {
		// helper.setOrderField(orderField);
		// }
		// if (ObjectUtils.isNotBlank(orderType)) {
		// helper.setOrderType(orderType);
		// }
		//

		Map<String, Object> dataMap = null;
		try {
			dataMap = this.userService.getUserDataGrid(helper);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return dataMap;
	}

	@RequestMapping(value = "/preAdd")
	public ModelAndView preAdd() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("user/user_add");
		return mv;
	}

	@RequestMapping(value = "/loadDepartmentList")
	@ResponseBody
	public List<Map<String, Object>> loadDepartmenList() {
		return userService.loadDepartmentList();
	}

	@RequestMapping(value = "/loadJobTitleList")
	@ResponseBody
	public List<Map<String, Object>> loadJobTitleList() {
		return userService.loadJobTitleList();
	}

	@RequestMapping(value = "/loadUserRoleList")
	@ResponseBody
	public Map<String, Object> loadUserRoleList(@RequestParam(value = "userId", required = false) Integer userId) {
		return userService.loadUserRoleList(userId);
	}

	@RequestMapping(value = "/addUser")
	@ResponseBody
	public Message save(User user, @RequestParam(value = "selectRoleIds", required = false) List<Integer> selectRoleIds)
			throws Exception {

		Message message = null;

		// System.out.println(user);
		// System.out.println(selectRoleIds);

		try {
			this.userService.save(user, selectRoleIds);
			message = coverMessage("200", "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			message = coverMessage("500", e.getMessage());
		}

		return message;
	}

	@RequestMapping(value = "/preModify/{userId}")
	public String preModify(@PathVariable(value = "userId") Integer userId, ModelMap modelMap) {
		User user = this.userService.getUserById(userId);
		modelMap.put("user", user);
		// System.out.println(user);
		return "user/user_edit";
	}

	@RequestMapping(value = "/modifyUser")
	@ResponseBody
	public Message modifyUser(User user,
			@RequestParam(value = "selectRoleIds", required = false) List<Integer> selectRoleIds) throws Exception {
		System.out.println(selectRoleIds);

		Message message = null;

		try {
			this.userService.modifyUser(user, selectRoleIds);
			message = coverMessage("200", "修改成功");
		} catch (Exception e) {
			message = coverMessage("500", e.getMessage());
			e.printStackTrace();
		}

		return message;
	}

	@RequestMapping(value = "/deleteUser")
	@ResponseBody
	public Message deleteUser(String decUserIds) throws Exception {
		System.out.println(decUserIds);
		Message message = null;

		try {
			this.userService.delectUsers(decUserIds);
			message = coverMessage("200", "删除成功");
		} catch (Exception e) {
			message = coverMessage("500", e.getMessage());
			e.printStackTrace();
		}

		return message;
	}

}
